// pages/searchpage/searchpage.js
var app = getApp()
const util = require('../../utils/util.js')
import Bmob from '../../utils/dist/Bmob-1.7.0.min.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shuru:'',
    nu:21,
    show:false,
    show1:true,
    showModal:false,
    isLoading: false,//正在加载中
    noMore: false//是否还有更多数据
  },
  input: function (e) {
    this.setData({ shuru: e.detail.value })
    this.setData({
      shuru: this.data.shuru
    })
  },
  send: function(){
    // 在页面中定义插屏广告
    let interstitialAd = null

    // 在页面onLoad回调事件中创建插屏广告实例
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-bad9eff9f6cc52e8'
      })
      interstitialAd.onLoad(() => { })
      interstitialAd.onError((err) => { })
      interstitialAd.onClose(() => { })
    }

    // 在适合的场景显示插屏广告
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
    
    wx.showLoading({ 
      title: '加载中', 
      })
    var that = this
    var nu = 5
    wx.request({
      url: 'https://jisusrecipe.market.alicloudapi.com/recipe/search?num=20&start=0&keyword=' + this.data.shuru,
      data: {
        x: '',
        y: ''
        //"pageNum": that.data.pagenum, //从数据里获取当前页数
        //"pageSize": 20, //每页显示条数
      },
      header: {
        'content-type': 'application/json', // 默认值
        "Authorization": "APPCODE 3930a9e0791146739618fd11f8ec15d4"
      },
      success(res) {
        console.log(res)
        wx.hideLoading()
        if (res.data.result == '') {
          that.setData({
            showModal:true,
          })
        
          /* wx.showModal({
            title: '提示',
            content: '没有你想要的菜',
            success(res) {
              if (res.confirm) {
                wx.switchTab({
                  //url: '../index/index'
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })  */
        } else {
          that.setData({
            ztsj: res.data.result.list, //合并后更新datalist
            show:true,
            show1:false

          })
          

        }

      }
    })

  },
  // 弹出层里面的弹窗
  ok: function () {
    this.setData({
      showModal: false,
      
    })
  },
  //页面跳转
  tiaozhuan: function(e){
    let item = e.currentTarget.dataset.item;
    let _str = JSON.stringify(item)

    wx.navigateTo({
      //url: e.currentTarget.dataset.path,
      url: '../search0/search0?shuju=' + _str,
    })


  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const query = Bmob.Query('class');
    query.find().then(res => {
      console.log(res)
      var searchOne = [];
      var searchTwo = [];
      var searchThree = [];
      var searchFour = [];
      var searchFive = [];
      for (var i = 0; i < res.length; i++) {
        searchOne.push(res[i].searchOne);
        searchTwo.push(res[i].searchTwo);
        searchThree.push(res[i].searchThree);
        searchFour.push(res[i].searchFour);
        searchFive.push(res[i].searchFive);
      }
      var searchNameArr = { searchOne, searchTwo, searchThree, searchFour, searchFive }
      this.setData({
        searchNameArr: searchNameArr
      })
      console.log(searchNameArr)

      const query = Bmob.Query('topclass');
      query.get('1EkFllly').then(res => {
        this.setData({
          one: res.one,
          two: res.two,
          three: res.three,
          four: res.four,
          five: res.five,
        })
      }).catch(err => {
        console.log(err)

      })
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '玩命加载中',
    })
    var that = this;
    var nu = that.data.nu
    wx.request({
      url: 'https://jisusrecipe.market.alicloudapi.com/recipe/search?num=20&keyword=' + this.data.shuru + "&start=" + nu,
      data: {
        x: '',
        y: ''
        //"pageNum": that.data.pagenum, //从数据里获取当前页数
        //"pageSize": 20, //每页显示条数
      },
      header: {
        'content-type': 'application/json', // 默认值
        "Authorization": "APPCODE 3930a9e0791146739618fd11f8ec15d4"
      },
      success(res) {
        if (res.data.result == '') {
          console.log("已经没有了")
          that.setData({
            noMore:true
          })

        } else {
          that.setData({
            isLoading: true
          })
          console.log(res)
          var arr1 = that.data.ztsj;
          console.log(arr1);
          var arr2 = res.data.result.list;
          console.log(arr2);
          arr1 = arr1.concat(arr2);
          console.log(arr1)
          that.setData({
            ztsj: arr1
          })
        }

      }
    })
    that.setData({
      nu: nu + 20
    })
    wx.hideLoading();


  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})